/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include "tiling/tiling_api.h"
#include "tiling/platform/platform_ascendc.h"
#include "../testcase_params.h"
#include "matmul_nbuffer33_custom_tiling.h"

namespace optiling {
bool ComputeTiling(optiling::TCubeTiling& tiling, matmul_tiling::MultiCoreMatmulTiling* cubeTiling,
    const int32_t M, const int32_t N, const int32_t K, int32_t blockDim, const bool isBias,
    const int32_t stepM, const int32_t stepN, const int32_t stepKa, const int32_t stepKb,
    const int32_t depthA1, const int32_t depthB1, const int32_t baseM, const int32_t baseN,
    const int32_t baseK)
{
    cubeTiling->SetDim(blockDim);
    cubeTiling->SetAType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
    cubeTiling->SetBType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT16);
    cubeTiling->SetCType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT);
    cubeTiling->SetBiasType(matmul_tiling::TPosition::GM, matmul_tiling::CubeFormat::ND, matmul_tiling::DataType::DT_FLOAT);
    cubeTiling->SetShape(M, N, K);
    cubeTiling->SetOrgShape(M, N, K);
    cubeTiling->EnableBias(isBias);
    cubeTiling->SetBufferSpace(-1, -1, -1);
    if (cubeTiling->GetTiling(tiling) == -1) {
        return false;
    }
    tiling.set_baseM(baseM);
    tiling.set_baseN(baseN);
    tiling.set_baseK(baseK);
    tiling.set_stepM(stepM);
    tiling.set_stepN(stepN);
    tiling.set_stepKa(stepKa);
    tiling.set_stepKb(stepKb);
    tiling.set_depthA1(depthA1);
    tiling.set_depthB1(depthB1);
    return true;
}

void GenerateTiling(const char *socVersion, uint8_t *buf)
{
    optiling::TCubeTiling tilingData;
    auto ascendcPlatform = platform_ascendc::PlatformAscendCManager::GetInstance(socVersion);
    auto aivCoreNum = platform_ascendc::PlatformAscendCManager::GetInstance()->GetCoreNum();
    matmul_tiling::MultiCoreMatmulTiling cubeTiling(*ascendcPlatform);
    bool res = ComputeTiling(tilingData, &cubeTiling, TESTCASE_PARAMS.m, TESTCASE_PARAMS.n, TESTCASE_PARAMS.k,
        aivCoreNum, TESTCASE_PARAMS.hasBias, TESTCASE_PARAMS.stepM, TESTCASE_PARAMS.stepN, TESTCASE_PARAMS.stepKa,
        TESTCASE_PARAMS.stepKb, TESTCASE_PARAMS.depthA1, TESTCASE_PARAMS.depthB1,
        TESTCASE_PARAMS.baseM, TESTCASE_PARAMS.baseN, TESTCASE_PARAMS.baseK);
    if (!res) {
        std::cout << "gen tiling failed" << std::endl;
    }
    uint32_t tilingSize = tilingData.GetDataSize();
    tilingData.SaveToBuffer(buf, tilingSize);
}
} // namespace optiling